/*
 * @Author: your name
 * @Date: 2021-08-09 18:33:40
 * @Description: In User Settings Edit
 * @FilePath: /wx-weapp/utils/enhancer/core/index.js
 */

import { Observer } from "./observe";
import Watcher from "./watcher";

export function initData(vm, isObserverData) {
  vm.__proto__.__isObserverData = isObserverData
  new Observer(vm.properties, vm);
}

export function initWatcher(vm) {
  let watch = vm.watch || vm.params.watch || {};
  Object.keys(watch).forEach((key) => new Watcher(vm, key, key, watch[key]));
}


export function initComputed(vm){
  let computed = vm.computed || vm.params.computed || {}
  Object.keys(computed).forEach(key=>{
    setInitComputedValue(vm,key,computed[key].call(vm))
    new Watcher(vm,key,computed[key],(val,oval)=>{
      setInitComputedValue(vm,key,val)
    })
  })
}

function setInitComputedValue(vm,key,value){
  vm.setData({
    [key]:value
  })
}
